﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using HHCommon;
using HHModel;
using HHBLL;

namespace HHWeb.SystemManage.News
{
    public partial class Dispose : Base
    {
        Help help = new Help();
        EncAndDec EncAndDec = new EncAndDec();
        HHBLL.News B = new HHBLL.News();
        HHModel.News M= new HHModel.News();
        HHCommon.PagePower pagepower = new HHCommon.PagePower();
        //变量定义
        public string Title = string.Empty;
        public string NewClass = string.Empty;
        public string NewClassName = string.Empty;
        public int NewClassID=0;
        public string NewSource = string.Empty;
        public string Author = string.Empty;
        public string PublishTime = string.Empty;
        public int ClickRate =0;
        public int Sort=0;
        public int Status=1;
        public string Creater = string.Empty;
        public string CreateTime = string.Empty;
        public string Command = "Add";//执行命令
        protected void Page_Load(object sender, EventArgs e)
        {
            string message = string.Empty;
            try
            {
                if (help.GetHttpMethodBool())
                {
                    Command = Request.Form["Command"].ToString();
                    switch (Command)
                    {
                        case "Add":
                            message = AddNews();
                            break;
                        case "Update":
                            message = UpdateNews();
                            break;
                        default:
                            break;
                    }
                    if (message.EndsWith("成功！"))
                        MessageBox.ShowAndRedirect(this.Page, message, "List.aspx");
                    else
                        MessageBox.ResponseScript(this.Page, message, "Javascript:history.go(-1)");
                }
                else
                {
                    if (help.IFPageParameter())
                    {
                        int NewID = Convert.ToInt32(Request.QueryString["ID"].ToString());
                        M = B.GetModel(NewID);
                        if (M != null)
                        {
                            Title = M.Title;
                            NewClassID = Convert.ToInt32(M.NewClassID);
                            NewSource = M.NewSource;
                            Author = M.Author;
                            PublishTime = Convert.ToDateTime(M.PublishTime).ToString("yyyy-MM-dd HH:mm:ss");
                            ClickRate = Convert.ToInt32(M.ClickRate);
                            Sort = Convert.ToInt32(M.Sort);
                            Status = Convert.ToInt32(M.Status);
                            this.FCKeditor1.Value = M.NewContent;
                            Creater = M.Creater;
                            CreateTime = Convert.ToDateTime(M.CreateTime).ToString("yyyy-MM-dd HH:mm:ss");
                            Command = "Update";
                        }
                        else
                        {
                            MessageBox.ShowAndRedirect(this.Page, "该数据不存在！", "List.aspx");
                            return;
                        }
                    }
                    else
                    {
                        PublishTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                        Creater = EncAndDec.DESDecrypting(help.GetCookie("UserCode"));
                        CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    }
                }
                //新闻类别（无限极下拉框）
                GetNewClass();
            }
            catch
            {
                MessageBox.ResponseScript(this.Page, "操作错误，请联系管理员！", "javascript:history.go(-1)");
                return;
            }
        }

        #region //新闻类别（无限极下拉框）
        public void GetNewClass()
        {
            NewClass = pagepower.GetPagePowerInfo("Select [ID] as ID, [NewClassName] as Name, [ParentID] as ParentID from NewClass with(nolock) where Status=1 order by sort desc");
        }
        #endregion

        #region //新闻添加
        public string AddNews()
        {
            M.Title = Request.Form["Title"].ToString();
            M.NewClassID =Convert.ToInt32(Request.Form["NewClass"]);
            string ClassName = Request.Form["NewClassName"].ToString();
            M.NewClassName = ClassName.Remove(0, ClassName.LastIndexOf('-')+1);
            M.NewSource = Request.Form["NewSource"].ToString();
            M.Author = Request.Form["Author"].ToString();
            M.PublishTime = Convert.ToDateTime(Request.Form["PublishTime"]);
            M.ClickRate = Convert.ToInt32(Request.Form["ClickRate"]);
            M.NewContent = this.FCKeditor1.Value;
            M.Sort = Convert.ToInt32(Request.Form["Sort"]);
            M.Status =Convert.ToInt32(Request.Form["Status"]);
            M.Creater = Request.Form["Creater"];
            M.CreateTime =Convert.ToDateTime(Request.Form["CreateTime"]);
            if (B.Add(M)> 0)
                return "添加成功！";
            else
                return "添加失败！";
        }
        #endregion

        #region //新闻修改
        public string UpdateNews()
        {
            M.ID = Convert.ToInt32(Request.QueryString["ID"].ToString());
            M.Title = Request.Form["Title"].ToString();
            M.NewClassID = Convert.ToInt32(Request.Form["NewClass"]);
            string ClassName = Request.Form["NewClassName"].ToString();
            M.NewClassName = ClassName.Remove(0, ClassName.LastIndexOf('-')+1);
            M.NewSource = Request.Form["NewSource"].ToString();
            M.Author = Request.Form["Author"].ToString();
            M.PublishTime = Convert.ToDateTime(Request.Form["PublishTime"]);
            M.ClickRate = Convert.ToInt32(Request.Form["ClickRate"]);
            M.NewContent = this.FCKeditor1.Value;
            M.Sort = Convert.ToInt32(Request.Form["Sort"]);
            M.Status = Convert.ToInt32(Request.Form["Status"]);
            M.Creater = Request.Form["Creater"];
            M.CreateTime = Convert.ToDateTime(Request.Form["CreateTime"]);
            if (B.Update(M))
                return "修改成功！";
            else
                return "修改失败！";
        }
        #endregion
    }
}